<?php
Class C_tree extends Cemvc_Control_Base
{
  private $db;
  public function __construct()
  {
    parent::__construct();
    $this->db = new M_qin_relation();
    /* $this->db->SetDbCodeNames("utf8"); */

    if(isset($_REQUEST['userid']) && $_REQUEST['userid'])
      {
	$db = new M_qin_users();
	$user = $db->get($_REQUEST['userid']);
	$region = new M_qin_region();
	$user['region'] = $user['region_code'] ? $region->get_full_name($user['region_code']) : "";
	$user['gender_str'] = $user['gender'] == MALE ? '男' : '女';
	$user["figureurl"] = M_Tools::get_figure_url($user["figureurl"], $user["gender"]);
	$this->view->assign("user", $user);
	$this->user = $user;
      }
    $this->view->assign("default_user", DEFAULT_USER_IN_HOME);
    $this->view->assign("USE_BETTER_BROWSER", USE_BETTER_BROWSER);
  }

  /* insert a user record into users table */
  public function index()
  {
    if (!isset($_REQUEST["userid"]) && !isset($_SESSION["id"]))
      {
	header("http/1.1 404 Not Found");
	header("Status: 404 Not Found"); 
	die();
      }

    $id = $_REQUEST["userid"];
    $req = new M_qin_request();
    if (isset($_SESSION["id"]))
      {
	$res = $req->get_request($_SESSION["id"]);
	$this->view->assign("messages", $res);
	$this->view->assign("login", "1");
      }
    $visit = new M_qin_visits();
    $visit->add_visit($id, isset($_SESSION["id"]) ? $_SESSION["id"] : 0);
    $views = $visit->get_visit($id);
    $this->view->assign("views", $views);

    $tbl = new M_qin_users();
    $user = $tbl->get($id);
    if (isset($_SESSION["id"]) && 
	($_SESSION["id"] == $user["creator"] ||	/* I create the user */
	 $_SESSION["id"] == $user["id"] ||	/* I am the user */
	 $_SESSION["creator"] == $user["creator"])) /* we were created with a same user */
      {
	$this->view->assign("authorized", 1);
      }

    /* $gender = isset($_REQUEST["gender"]) ? $_REQUEST["gender"] : $_SESSION["gender"]; */
    $this->view->assign("userid", $id);
    /* $this->view->assign("gender", $gender); */
    $this->view->assign("QIN_TREE_WIDTH", QIN_TREE_WIDTH);
    $this->view->assign("QIN_TREE_HEIGHT", QIN_TREE_HEIGHT);

    $data = $this->db->get_family_relation($id, RESOURCE_STATE_INIT);

    include_once WEB_ROOT."/M/Tree.php";
    $width = getTreeWidth($data)* (THUMB_WIDTH + THUMB_MARGIN_X);
    $width = $width > (960 - 200) ? $width : (960 - 200);
    $height = getTreeDepth($data)* (MEMBER_BLOCK_HEIGHT + RELATIVE_LINE_HEIGHT);
    $this->view->assign("height", $height );
    $this->view->assign("width", $width+THUMB_MARGIN_X); /* add padding */
    $this->view->assign("tree", drawMemberTree($data, $width/2, 10));

    $this->display();
  }
  
  public function get_family_tree()
  {
    if (!isset($_REQUEST["userid"]) || empty($_REQUEST["userid"])
	/*  || */
	/* !isset($_REQUEST["gender"]) || empty($_REQUEST["gender"]) */
	)
      die("You must specified the user id.");
    
    /* call get tree */
    $data = $this->db->get_family_relation($_REQUEST["userid"], RESOURCE_STATE_INIT);

    header("Content-type: text/html; charset=utf-8");
    echo urldecode(json_encode($data["tree"]));
  }
  
}
